package com.ssyt.yt1.day27;

/**
 * 终止线程：Thread类里的内置stop()方法已过时，现以标记变量来控制
 *
 * @author Hsu.
 * @since 2015年12月31日
 */
public class ThreadStopDemo extends Thread {

	private boolean flag = true;

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	@Override
	public void run() {
		for (int i = 0; i < 10 && flag; i++) {
			try {
				Thread.sleep(100);
				System.out.println("Sub thread:" + this.getName());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		ThreadStopDemo thread = new ThreadStopDemo();
		thread.start();

		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		// This method is inherently unsafe.
		// thread.stop();

		// Many uses of <code>stop</code> should be replaced by code that simply
		// modifies some variable to indicate that the target thread should stop
		// running.
		// thread.setFlag(false);
	}
}
